package com.tianyang.yukuaifu.business.main.view;

import android.content.Context;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.tianyang.yukuaifu.R;
import com.tianyang.yukuaifu.base.BaseTitleActivity;
import com.tianyang.yukuaifu.business.main.adapter.StoreManageAdapter;
import com.tianyang.yukuaifu.business.main.bean.ChannelOutput;
import com.tianyang.yukuaifu.business.main.bean.UserCenterOutput;
import com.tianyang.yukuaifu.business.main.bean.UserStoreOutput;
import com.tianyang.yukuaifu.business.main.contract.StoreManageContract;
import com.tianyang.yukuaifu.business.main.presenter.StoreManagePresenter;
import com.tianyang.yukuaifu.common.cache.CacheManage;
import com.tianyang.yukuaifu.common.cache.CacheModel;
import com.tianyang.yukuaifu.common.utils.CommonUtils;
import com.tianyang.yukuaifu.module.dialog.AddStoreDialog;
import com.tianyang.yukuaifu.module.dialog.PosChannelDialog;
import com.tianyang.yukuaifu.module.dialog.RemindDialog;
import com.tianyang.yukuaifu.module.dialog.interfaces.ICallbackListener;
import com.tianyang.yukuaifu.module.dialog.interfaces.TwoBtnClickListener;
import com.tianyang.yukuaifu.module.immersionbar.ImmersionBar;
import com.tianyang.yukuaifu.module.widget.VerticalSwipeRefreshLayout;
import com.tianyang.yukuaifu.utils.SystemBarHelper;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Description：店铺门店管理
 * Created by xyj on 2024/3/11
 */
public class StoreManageActivity extends BaseTitleActivity implements StoreManageContract.View {

    @BindView(R.id.top_view)
    View topView;
    @BindView(R.id.swipeRefreshLayout)
    VerticalSwipeRefreshLayout vSwipeRefreshLayout;
    @BindView(R.id.recyclerView)
    RecyclerView vRecyclerview;

    private RelativeLayout emptyView;

    private Button addStoreTv;

    private StoreManageAdapter vStoreManageAdapter;

    private AddStoreDialog vAddStoreDialog;

    private PosChannelDialog vPosChannelDialog;

    private StoreManageContract.Presenter mPresenter;

    private List<ChannelOutput> channelOutputs = new ArrayList<>();

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_store_manage;
    }

    @Override
    protected void initView() {
        super.initView();
        int barHeight = SystemBarHelper.getStatusBarHeight(getContext());
        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) topView.getLayoutParams();
        params.height = barHeight;
        topView.setLayoutParams(params);

        UserCenterOutput output = CacheManage.getInstance().getCache(CacheModel.USER_CENTER_KEY);
        vRecyclerview.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));
        vStoreManageAdapter = new StoreManageAdapter(output != null?output.getMemberId():-1);
        View footerView = View.inflate(mContext, R.layout.layout_store_manage_footer, null);
        emptyView = footerView.findViewById(R.id.emptyView);
        addStoreTv = footerView.findViewById(R.id.confirmTv);
        vStoreManageAdapter.addFooterView(footerView);
        vRecyclerview.setAdapter(vStoreManageAdapter);

        mPresenter = new StoreManagePresenter(this);
        mPresenter.getStoreManageList();
        mPresenter.getChannelList();
    }

    @Override
    protected void setListener() {
        super.setListener();
        vSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mPresenter.getStoreManageList();
            }
        });
        vStoreManageAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                if (view.getId() == R.id.changeIv) {
                    UserStoreOutput output = ((StoreManageAdapter) adapter).getItem(position);
                    if (output != null) {
                        new RemindDialog(mContext)
                                .title("提示")
                                .content("确认切换到"+output.getUsername()+"店铺么?")
                                .listener(new TwoBtnClickListener() {
                                    @Override
                                    public void onLeftClick() {

                                    }

                                    @Override
                                    public void onRightClick() {
                                        mPresenter.getAnotherStoresToken(output.getId());
                                    }
                                })
                                .show();
                    }
                }
            }
        });
        addStoreTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showMerchantTypeDialog();
            }
        });
    }

    @OnClick({R.id.backIv})
    public void onClick(View view) {
        if (view.getId() == R.id.backIv) {
            finish();
        }
    }

    /**
     * Description:显示商户类型弹框
     */
    private void showMerchantTypeDialog() {
        if (vAddStoreDialog == null) {
            vAddStoreDialog = new AddStoreDialog(getContext());
        }
        vAddStoreDialog.setListener(new ICallbackListener<String>() {
            @Override
            public void callback(String merchantType) {
                showPosChannelDialog(merchantType);
            }
        });
        vAddStoreDialog.show();
    }

    private void showPosChannelDialog(String merchantType) {
        if (vPosChannelDialog == null) {
            vPosChannelDialog = new PosChannelDialog(getContext(), channelOutputs, true);
        }
        vPosChannelDialog.setListener(new ICallbackListener<ChannelOutput>() {
            @Override
            public void callback(ChannelOutput channelOutput) {
                new RemindDialog(mContext)
                        .title("温馨提示")
                        .content("确认添加《"+ channelOutput.getName() +"》" + CommonUtils.getMerchantName(Integer.parseInt(merchantType)) +"店铺么?")
                        .listener(new TwoBtnClickListener() {
                            @Override
                            public void onLeftClick() {

                            }

                            @Override
                            public void onRightClick() {
                                mPresenter.createNewStoreMerchant(merchantType, channelOutput.getCode());
                            }
                        }).show();
            }
        });
        vPosChannelDialog.show();
    }

    @Override
    public void showChannelView(List<ChannelOutput> outputs) {
        if (outputs != null) {
            channelOutputs = outputs;
        }
    }

    @Override
    public void showStoreManageListView(List<UserStoreOutput> outputs) {
        if (vSwipeRefreshLayout.isRefreshing()) {
            vSwipeRefreshLayout.setRefreshing(false);
        }
        if (outputs != null && outputs.size() > 0) {
            vStoreManageAdapter.setNewData(outputs);
            emptyView.setVisibility(View.GONE);
        } else {
            vStoreManageAdapter.setNewData(new ArrayList<>());
            emptyView.setVisibility(View.VISIBLE);
        }
    }

    @Override
    protected void initImmersionBar() {
        mImmersionBar = ImmersionBar.with(this);
        mImmersionBar.statusBarColorTransformEnable(true)
                .statusBarDarkFont(true, 0.5f)
                .init();
    }

    @Override
    protected boolean hasCommonTitle() {
        return false;
    }

    @Override
    protected boolean hasBindEventBus() {
        return false;
    }

    @Override
    public void setPresenter(StoreManageContract.Presenter presenter) {
        this.mPresenter = presenter;
    }

    @Override
    public Context getContext() {
        return this;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (vAddStoreDialog != null && vAddStoreDialog.isShowing()) {
            vAddStoreDialog.dismiss();
            vAddStoreDialog = null;
        }
        if (vPosChannelDialog != null && vPosChannelDialog.isShowing()) {
            vPosChannelDialog.dismiss();
            vPosChannelDialog = null;
        }
        if (mPresenter != null) {
            mPresenter.onDestroyPresenter();
            mPresenter = null;
        }
     }
}
